package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.ProductDetailsMapper;
import com.zhentao.mapper.ProductMapper;
import com.zhentao.mapper.ProductSkuMapper;
import com.zhentao.mapper.ProductUnitMapper;
import com.zhentao.minio.service.FileStorageService;
import com.zhentao.model.dto.product.ProductDto;
import com.zhentao.model.entity.base.ProductUnit;
import com.zhentao.model.entity.product.Product;
import com.zhentao.model.entity.product.ProductDetails;
import com.zhentao.model.entity.product.ProductSku;
import com.zhentao.model.vo.common.Result;
import com.zhentao.model.vo.common.ResultCodeEnum;
import com.zhentao.service.ProductService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService{

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private ProductUnitMapper productUnitMapper;

    //    minio注入配置
    @Resource
    private FileStorageService fileStorageService;

    @SneakyThrows
    @Override
    public Result file(MultipartFile file) {
        String s = fileStorageService.uploadPicFile(file.getOriginalFilename(), file.getInputStream());
        return Result.build(s, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result productUnit() {

        LambdaQueryWrapper<ProductUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductUnit::getIsDeleted, 1);
        List<ProductUnit> productUnits = productUnitMapper.selectList(wrapper);
        return Result.build(productUnits, ResultCodeEnum.SUCCESS);
    }


    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<Product> byPage = productMapper.findByPage(productDto);
        return new PageInfo<>(byPage);
    }

    @Transactional
    @Override
    public Result productSave(Product product) {

        //添加商品信息
        product.setAuditStatus(0);
        product.setStatus(0);
        product.setCreateTime(new Date());
        product.setIsDeleted(0);
        productMapper.save(product);

        //保存商品sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        if (productSkuList != null && productSkuList.size() > 0) {
            for (int i = 0;i < product.getProductSkuList().size(); i++) {
//                获取ProductSku对象
//        ProductSku productSku = new ProductSku();
                ProductSku productSku = productSkuList.get(i);
//                构建 skuCode
                productSku.setSkuCode(product.getId()+"_"+i);
//                设置商品id
                productSku.setProductId(Long.valueOf(product.getId()));
                productSku.setSkuName(product.getName()+productSku.getSkuSpec());
//                设置销量
                productSku.setSaleNum(0);
                productSku.setStatus(0);
//                保存数据
                productSkuMapper.insert(productSku);
            }
        }


//        保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(Long.valueOf(product.getId()));
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);

        return Result.build("添加成功",ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result productFindOne(Long id) {

        // 根据id查询商品数据
        Product product = productMapper.selectById(id);

        // 根据商品的id查询sku数据
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);

        // 根据商品的id查询商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(Long.valueOf(product.getId()));
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    @Transactional
    @Override
    public Result productUpdate(Product product) {

        // 修改商品基本数据
        productMapper.updateById(product);

        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateById(productSku);
        });

        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(Long.valueOf(product.getId()));
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
        return Result.build("操作成功", ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result productDelete(Long id) {

//        删除商品的单条信息
        int i = productMapper.deleteById(id);
        System.out.println(i+"+++++++++++++++++++++++++++++++");
        //删除相关的商品详情的信息
        LambdaQueryWrapper<ProductDetails> eq = new LambdaQueryWrapper<ProductDetails>().eq(ProductDetails::getProductId, id);
        int delete = productDetailsMapper.delete(eq);
        System.out.println("------------------");
        //删除相关的商品规格信息
        LambdaQueryWrapper<ProductSku> eq1 = new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, id);
        int delete1 = productSkuMapper.delete(eq1);
        System.out.println(delete1+"*******************");
        return Result.build("操作成功", ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result productExamine(Long id, Integer auditStatus) {
        System.err.println(id +" "+ auditStatus);
        Product product = productMapper.selectById(id);
        if (auditStatus == 1) {
            product.setStatus(1);
            product.setAuditMessage("审核通过");
        }else if (auditStatus == -1) {
            product.setStatus(-1);
            product.setAuditMessage("审核不通过");
        }
        productMapper.updateById(product);
        return Result.build("操作成功", ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result productStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(Long.valueOf(String.valueOf(id)));
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);

        return Result.build("操作成功", ResultCodeEnum.SUCCESS);
    }
}
